/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package notificationcenter;

import abstractClasses.AbstractEntityList;
import entities.Notification;
import entities.User;
import facades.NotificationFacade;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.enterprise.event.Reception;
import javax.enterprise.event.TransactionPhase;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import qualifiers.CreatedEntityEvent;
import qualifiers.EditedEntityEvent;
import qualifiers.LoggedIn;
import qualifiers.PreEditEntityEvent;

/**
 *
 * @author Alvaro
 */
@Named(value = "notificationsUserBean")
@SessionScoped
public class NotificationsUserBean extends AbstractEntityList<Notification, NotificationFacade> implements Serializable {
    
    @Inject 
    private NotificationFacade notificationFacade;
    @Inject
    @CreatedEntityEvent
    private Event<Notification> createdNotificationEvent;
    @Inject
    @EditedEntityEvent
    private Event<Notification> editedNotificationEvent;
    @Inject
    @PreEditEntityEvent
    private Event<Notification> preEditNotificationEvent;
    @Inject
    @LoggedIn
    private User user;
    
    @Override
    public void initEntityList(){
        if(user.getNotificationList()==null){
            user.setNotificationList(new ArrayList<Notification>());
        }
        setEntityList(user.getNotificationList());
        Collections.reverse(getEntityList());
    }
    
    public NotificationsUserBean() {
    } 
    
    public void markAllNotificationsAsView(){
        for(Notification n :getEntityList()){
           if(!n.getIsNotificated()){
                n.setIsNotificated(true);
                preEditNotificationEvent.fire(n);
                notificationFacade.edit(n);
                editedNotificationEvent.fire(n);
            } 
        }
    }
    
    public void notificationRead(){
        if(getChosenEntity()!=null && !getChosenEntity().getIsNotificated()){
            getChosenEntity().setIsNotificated(true);
            preEditNotificationEvent.fire(getChosenEntity());
            notificationFacade.edit(getChosenEntity());
            editedNotificationEvent.fire(getChosenEntity());
        }
    }
    
    @Override
    public void onCreatedEntity(@Observes(during = TransactionPhase.AFTER_SUCCESS,notifyObserver = Reception.IF_EXISTS) @CreatedEntityEvent Notification notification){
       if(!getEntityList().contains(notification)){
           getEntityList().add(0, notification);
       }
    }
    
    public void recieveNotification(){
        FacesContext context = FacesContext.getCurrentInstance();
        Map map = context.getExternalContext().getRequestParameterMap();
        String idNotificationString = (String) map.get("idNotification");
        Integer idNotification = null;
        try {
            idNotification=new Integer(idNotificationString);
        } catch (NumberFormatException e) {
        }
        Notification notification = null;
        if(idNotification!=null){
            notification = notificationFacade.find(idNotification);
            createdNotificationEvent.fire(notification);
        }
        else{
            System.out.println("Notificación nula");
        }
        String notificationMessage=notification.getDescription();
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, notificationMessage, notificationMessage);  
        context.addMessage(null, message); 
        context.renderResponse();
    }

}
